<template>
  <el-header class="header">
    <el-row :gutter="20" justify="space-between" align="middle">
      <el-col :span="4" :xs="5">
        <div class="logo-container">
          <span class="logo-text">Zero</span>
        </div>
      </el-col>
      
      <!-- 集成到header的天气和时间显示 -->
      <el-col :span="6" :xs="7" class="weather-time-section">
        <div class="weather-display">
          <div class="weather-icon-container">
            <el-icon class="weather-icon"><CloudSun /></el-icon>
          </div>
          <span class="temperature">{{ weather.temperature }}°</span>
          <span class="weather-condition desktop-only">{{ weather.condition }}</span>
        </div>
        <div class="time-divider desktop-only"></div>
        <div class="time-display desktop-only">
          <span class="current-time">{{ currentTime }}</span>
          <span class="current-date">{{ currentDate }}</span>
        </div>
      </el-col>
      
      <el-col :span="14" :xs="12" class="nav-col">
        <!-- 移动端搜索框 -->
        <el-input v-if="$isMobile && showSearch" v-model="searchQuery" placeholder="搜索..." clearable
          @change="handleSearch" class="search-input">
          <template #prefix>
            <el-icon class="search-icon">
              <Search />
            </el-icon>
          </template>
        </el-input>

        <!-- 桌面端导航 -->
        <el-menu v-if="!$isMobile" mode="horizontal" :collapse="false" class="nav" background-color="#ffffff"
          text-color="#1f2937" active-text-color="#10b981" :router="true" :default-active="activeIndex">
          <!-- 搜索框 -->
          <transition name="fade">
            <el-input v-if="showSearch" v-model="searchQuery" placeholder="搜索..." clearable @change="handleSearch"
              class="search-input" ref="searchInput">
              <template #prefix>
                <el-icon class="search-icon">
                  <Search />
                </el-icon>
              </template>
            </el-input>
          </transition>

          <!-- 普通用户导航 -->
          <template v-if="!isAdmin">
            <el-menu-item v-for="item in userMenuItems" :key="item.path" :index="item.path">
              <component :is="item.icon" class="menu-icon" :size="16" />
              <span>{{ item.label }}</span>
            </el-menu-item>
          </template>
        </el-menu>
      </el-col>
    </el-row>
  </el-header>

  <!-- 移动端底部导航栏 -->
  <div v-if="$isMobile" class="mobile-bottom-nav">
    <div v-for="item in userMenuItems" :key="item.path" :class="['nav-item', { active: activeIndex === item.path }]"
      @click="handleTabChange(item.path)">
      <component :is="item.icon" class="menu-icon" :size="20" />
      <span class="nav-label">{{ item.label }}</span>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted, onUnmounted,getCurrentInstance } from 'vue';
import { useRoute, useRouter } from 'vue-router';
import { 
  Home, Users, Settings, User, Search, CloudSun, 
  CloudRain, CloudSnow, CloudLightning, Sun, Moon
} from 'lucide-vue-next';
const globObj = getCurrentInstance().appContext.config.globalProperties;

const props = defineProps({
  showSearch: {
    type: Boolean,
    default: false,
  },
  isAdmin: {
    type: Boolean,
    default: false,
  }
});

const emit = defineEmits(['search']);

const searchQuery = ref('');
const searchInput = ref(null);
const currentTime = ref('');
const currentDate = ref('');

// 模拟天气数据
const weather = ref({
  temperature: 24,
  condition: '晴朗',
  location: '北京市',
  icon: CloudSun
});

const route = useRoute();
const router = useRouter();

const activeIndex = computed(() => route.path);

const handleSearch = () => {
  emit('search', searchQuery.value);
};

const handleTabChange = (path) => {
  router.push(path);
};


const userMenuItems = [
  { path: '/', label: '首页', icon: Home },
  { path: '/community', label: '社区', icon: Users },
  { path: '/userInfo', label: '我的', icon: User }, 
  { path: globObj.$isMobile ? '/publish' : '/system', label: globObj.$isMobile ? '发布' : '后台', icon: Settings },
];

// 更新时间的函数
const updateTime = () => {
  const now = new Date();
  
  // 格式化时间 HH:MM
  const hours = now.getHours().toString().padStart(2, '0');
  const minutes = now.getMinutes().toString().padStart(2, '0');
  currentTime.value = `${hours}:${minutes}`;
  
  // 格式化日期 MM月DD日 星期几
  const month = (now.getMonth() + 1);
  const day = now.getDate();
  const weekdays = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
  const weekday = weekdays[now.getDay()];
  currentDate.value = `${month}月${day}日 ${weekday}`;
  
  // 根据时间更新天气图标（模拟）
  const hour = now.getHours();
  if (hour >= 6 && hour < 18) {
    weather.value.icon = CloudSun;
  } else {
    weather.value.icon = Moon;
  }
};

let timeInterval;

onMounted(() => {
  updateTime();
  timeInterval = setInterval(updateTime, 60000); // 每分钟更新一次
  
  // 这里可以添加获取实际天气数据的API调用
  // fetchWeatherData();
});

onUnmounted(() => {
  clearInterval(timeInterval);
});
</script>

<style scoped lang="scss">
.header {
  position: fixed;
  z-index: 10;
  top: 0;
  left: 0;
  width: 100%;
  border-bottom: 1px solid rgba(229, 231, 235, 0.5);
  background-color: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);

  .el-row {
    width: 100%;
    padding: 0 24px;
  }
}

// 标志样式
.logo-container {
  display: flex;
  align-items: center;
  position: relative;
  
  .logo-text {
    font-size: 24px;
    font-weight: 700;
    letter-spacing: 1px;
    background: linear-gradient(135deg, #10b981 0%, #0ea5e9 100%);
    -webkit-background-clip: text;
    -webkit-text-fill-color: transparent;
    position: relative;
    z-index: 1;
  }
  
  .logo-accent {
    position: absolute;
    width: 8px;
    height: 8px;
    background: #10b981;
    border-radius: 50%;
    bottom: 2px;
    right: -8px;
    box-shadow: 0 0 10px rgba(16, 185, 129, 0.5);
  }
}

// 天气和时间显示部分 - 直接集成到header
.weather-time-section {
  display: flex;
  align-items: center;
  justify-content: center;
  height: 100%;
}

.weather-display {
  display: flex;
  align-items: center;
  
  .weather-icon-container {
    width: 32px;
    height: 32px;
    border-radius: 50%;
    background: linear-gradient(135deg, #fbbf24 0%, #f59e0b 100%);
    display: flex;
    align-items: center;
    justify-content: center;
    margin-right: 10px;
    box-shadow: 0 2px 6px rgba(245, 158, 11, 0.3);
    
    .weather-icon {
      color: white;
      width: 18px;
      height: 18px;
    }
  }
  
  .temperature {
    font-size: 18px;
    font-weight: 600;
    color: #1f2937;
    margin-right: 6px;
  }
  
  .weather-condition {
    font-size: 13px;
    color: #6b7280;
  }
}

.time-divider {
  width: 1px;
  height: 24px;
  background: linear-gradient(to bottom, transparent, rgba(209, 213, 219, 0.8), transparent);
  margin: 0 16px;
}

.time-display {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  
  .current-time {
    font-size: 18px;
    font-weight: 600;
    color: #1f2937;
    line-height: 1.2;
  }
  
  .current-date {
    font-size: 12px;
    color: #6b7280;
    margin-top: 2px;
  }
}

.nav-col {
  display: flex;
  align-items: center;
  justify-content: flex-end;
}

.nav {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  border-bottom: none;

  .search-input {
    width: 220px;
    margin-right: 16px;
    transition: all 0.3s ease;

    :deep(.el-input__wrapper) {
      background-color: rgba(243, 244, 246, 0.7);
      border: 1px solid #e5e7eb;
      border-radius: 12px;
      box-shadow: none;
      height: 40px;

      &:hover {
        border-color: #10b981;
      }

      &:focus-within {
        border-color: #10b981;
        box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.2);
      }
    }

    :deep(.el-input__inner) {
      color: #1f2937;
      font-size: 14px;
    }

    :deep(.el-input__clear) {
      color: #6b7280;

      &:hover {
        color: #10b981;
      }
    }
  }

  .search-icon {
    color: #6b7280;
    transition: color 0.3s ease;

    &:hover {
      color: #10b981;
    }
  }

  :deep(.el-menu-item) {
    height: 64px;
    display: flex;
    align-items: center;
    font-size: 14px;
    transition: all 0.2s ease;
    border: none;
    padding: 0 16px;
    margin: 0 2px;
    border-radius: 8px;

    &:hover {
      color: #10b981;
      background-color: rgba(16, 185, 129, 0.08);

      .menu-icon {
        color: #10b981;
      }
    }

    &.is-active {
      color: #10b981;
      background-color: rgba(16, 185, 129, 0.12);
      font-weight: 600;

      .menu-icon {
        color: #10b981;
      }
    }
  }
}

.menu-icon {
  width: 18px;
  height: 18px;
  margin-right: 8px;
  color: #4b5563;
  transition: color 0.2s ease;
}

// 移动端底部导航栏
.mobile-bottom-nav {
  position: fixed;
  z-index: 10;
  bottom: 0;
  left: 0;
  width: 100%;
  height: 64px;
  background-color: rgba(255, 255, 255, 0.95);
  backdrop-filter: blur(10px);
  border-top: 1px solid rgba(229, 231, 235, 0.5);
  display: flex;
  justify-content: space-around;
  align-items: center;
  box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.05);
  padding: 0 8px;

  .nav-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    font-size: 12px;
    color: #6b7280;
    transition: all 0.3s ease;
    cursor: pointer;
    flex: 1;
    padding: 8px 0;
    position: relative;

    &.active {
      color: #10b981;

      .menu-icon {
        color: #10b981;
      }
      
      &::after {
        content: '';
        position: absolute;
        bottom: 0;
        left: 50%;
        transform: translateX(-50%);
        width: 20px;
        height: 3px;
        background: #10b981;
        border-radius: 3px 3px 0 0;
      }
    }

    .menu-icon {
      width: 22px;
      height: 22px;
      margin-bottom: 4px;
      color: #6b7280;
      transition: color 0.2s ease;
    }

    .nav-label {
      font-size: 12px;
      font-weight: 500;
      line-height: 1.2;
      margin-right: 8px;
    }
  }
}

// 桌面端专用元素
.desktop-only {
  display: inline-flex;
}

// 移动端样式
@media (max-width: 768px) {
  .header {
    height: 60px;
    
    .el-row {
      padding: 0 12px;
    }
  }
  
  .logo-container .logo-text {
    font-size: 20px;
  }
  
  .weather-time-section {
    display: flex; // 在移动端显示天气
    justify-content: flex-start;
  }
  
  .desktop-only {
    display: none; // 在移动端隐藏部分元素
  }
  
  .weather-display {
    .weather-icon-container {
      width: 28px;
      height: 28px;
      margin-right: 6px;
      
      .weather-icon {
        width: 16px;
        height: 16px;
      }
    }
    
    .temperature {
      font-size: 16px;
    }
  }
  
  .nav {
    display: none;
  }

  .search-input {
    height: 36px;
    width: 100% !important; // 确保搜索框占满可用空间
    max-width: none;
    margin-left: 0;
    transition: all 0.3s ease;

    :deep(.el-input__wrapper) {
      background-color: rgba(243, 244, 246, 0.7);
      border: 1px solid #e5e7eb;
      border-radius: 12px;
      box-shadow: none;

      &:hover {
        border-color: #10b981;
      }

      &:focus-within {
        border-color: #10b981;
        box-shadow: 0 0 0 2px rgba(16, 185, 129, 0.2);
      }
    }

    :deep(.el-input__inner) {
      color: #1f2937;
      font-size: 14px;
    }

    :deep(.el-input__clear) {
      color: #6b7280;

      &:hover {
        color: #10b981;
      }
    }
  }
  
  .nav-col {
    padding-left: 8px;
  }
}

// 过渡效果
.fade-enter-active,
.fade-leave-active {
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.fade-enter-from,
.fade-leave-to {
  opacity: 0;
  transform: translateX(-10px);
}
</style>

